package com.store.pet.petstore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.adapter.PetAdapter;
import com.store.pet.petstore.bean.PetListBean;
import com.store.pet.petstore.util.Constant;
import com.store.pet.petstore.util.GsonUtils;
import com.store.pet.petstore.util.OKHttpUtils;
import com.store.pet.petstore.util.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.store.pet.petstore.activity.PetListActivity.BUY_SUCCESS;

public class SearchActivity extends BaseActivity {

    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.search_et)
    EditText searchEt;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recycler_search)
    RecyclerView recyclerSearch;

    private List<PetListBean.ContentBean.PetlistBean> list = new ArrayList<>();  //宠物列表集合

    private PetAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_search;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeLinearStatusBar(myStatus);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationIcon(R.drawable.fanhui);  //设置左上角图标
        toolbar.setNavigationOnClickListener(view -> finish());  //退出

        recyclerSearch.setLayoutManager(new LinearLayoutManager(this));
        recyclerSearch.addItemDecoration(new SpaceItemDecoration(30));
        adapter = new PetAdapter(R.layout.item_pet_rv,list);
        recyclerSearch.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.pet_item:
                        Intent intent = new Intent(SearchActivity.this,PetDetailActivity.class);
                        intent.putExtra("pet",list.get(position));
                        startActivityForResult(intent,BUY_SUCCESS);
                        break;
                }
            }
        });
    }

    @Override
    public void loadData() {

    }

    @OnClick(R.id.search)
    public void onViewClicked() {
        list.clear();
        String string = searchEt.getText().toString();  //搜索关键词
        if(string.equals("")){
            ToastUtils.showShort("请输入关键词进行搜索");
        }else {
            Map<String,Object> map = new HashMap<>();
            map.put("name",string);
            OKHttpUtils.requestData(Constant.QUERY_COMMODITY_BY_NAME, map, new OKHttpUtils.ServerCallBack() {
                @Override
                public void onSuccess(String data) {
                    PetListBean bean = GsonUtils.parsePetListBean(data);
                    if(bean.getContent().getPetlist().size() != 0){
                        list.addAll(bean.getContent().getPetlist());
                    }else {
                        ToastUtils.showShort("暂无此宠物信息，请更换关键词");
                        searchEt.setText("");
                    }
                    /**
                     * 在主线程刷新UI
                     */
                    new Thread(() -> runOnUiThread(() -> adapter.notifyDataSetChanged())).start();
                    hideKeyboard(getCurrentFocus());
                }

                @Override
                public void onFailed(String message) {

                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data != null){
            switch (requestCode){
                case BUY_SUCCESS:
                    finish();
                    break;
            }
        }
    }
}
